//
//  UIImage+FZExtension.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/28/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

extension UIImage{

    //MARK: 截屏
    static func getScreenIMG() -> UIImage{
        
        let windows = UIApplication.sharedApplication().keyWindow!
        //开启图形上下文
//        UIGraphicsBeginImageContext(windows.bounds.size)
        UIGraphicsBeginImageContextWithOptions(windows.bounds.size, false, 0.0)
        //把当前屏幕绘制到屏幕
        windows.drawViewHierarchyInRect(windows.bounds, afterScreenUpdates: false)
        //得到IMG
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        return image
    }
    
    //裁剪图片，默认从宽度裁剪，如果高大于宽度与
    static func cutIMG(img: UIImage, scale: CGFloat, backCloure: (img: UIImage)->()){
        //以最短的的值做裁剪，默认裁剪单位是600
        var size: CGSize
        if img.size.height < img.size.width {
            
            let h = (scale * img.size.height) / img.size.width
            size = CGSizeMake(scale, h)
        }else{
            let w = (scale * img.size.width) / img.size.height
            size = CGSizeMake(w, scale)
        }
        
        UIGraphicsBeginImageContext(size)
        img.drawInRect(CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
         
        backCloure(img: image)
    }
    
    //得到指定路径下的图片
    static func getEmoticonImage(path: String) -> UIImage{
        
        
       return UIImage(named: path, inBundle: FZEmoticonTool.sharedTool.emoticonBundle, compatibleWithTraitCollection: nil)!
    }

    
    
    
    
}